package topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-04-01
 * Time: 13:52
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("1.95.194.117");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("wangkun");
        connectionFactory.setPassword("200411");
        connectionFactory.setVirtualHost("study");
        Connection connection = connectionFactory.newConnection();

        //2.通信
        Channel channel = connection.createChannel();

        //3.声明交换机
        channel.exchangeDeclare("topic", BuiltinExchangeType.TOPIC,true);

        //4.声明队列
        channel.queueDeclare("queue1",true,false,false,null);
        channel.queueDeclare("queue2",true,false,false,null);

        //5.绑定交换机和队列
        channel.queueBind("queue1","topic","*.a.*");
        channel.queueBind("queue2","topic","*.*.b");
        channel.queueBind("queue2","topic","c.#");

        //6.发送消息
        String message1 = "hello topic  my routing key is a" ;
        channel.basicPublish("topic","ae.a.f",null,message1.getBytes());//转发Q1

        String message2 = "hello topic  my routing key is b" ;
        channel.basicPublish("topic","ef.a.b",null,message2.getBytes());//转发Q1和Q2

        String message3 = "hello topic  my routing key is c" ;
        channel.basicPublish("topic","c.ef.d",null,message3.getBytes());//转发Q2

        //7.资源关闭
        channel.close();
        connection.close();

    }
}
